﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using H2.NET.Business;
using H2.NET.Web.Pages.SMD.Service;
using H2.NET.Web.Workflow.Helper;

namespace H2.NET.Web.Pages.SMD
{
    public partial class uc_head : TaskControl
    {
        public string applicationNo
        {
            get
            {
                return ViewState["applicationNo"] == null ? "" : (string)ViewState["applicationNo"];
            }
            set
            {
                ViewState["applicationNo"] = value;
            }
        }

        public string date
        {
            get
            {
                return ViewState["date"] == null ? "" : (string)ViewState["date"];
            }
            set
            {
                ViewState["date"] = value;
            }
        }

        public string applicatorName
        {
            get
            {
                return ViewState["applicator"] == null ? "" : (string)ViewState["applicator"];
            }
            set
            {
                ViewState["applicator"] = value;
            }
        }

        public bool isEapprove
        {
            get
            {
                return ViewState["isEapprove"] == null ? false : (bool)ViewState["isEapprove"];
            }
            set
            {
                ViewState["isEapprove"] = value;
            }
        }

        public string summary
        {
            get { return ViewState["summary"] == null ? "" : ViewState["summary"].ToString(); }
            set { ViewState["summary"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(applicationNo))
                {
                    applicationNo = TaskAPI.GenerateApplicationNo();
                }
                if (string.IsNullOrEmpty(date))
                {
                    date = DateTime.Now.ToString("yyyy-MM-dd");
                }
                if (string.IsNullOrEmpty(applicatorName))
                {
                    applicatorName = TaskAPI.GetVariableBySingleValue("Applicator");
                    if (string.IsNullOrEmpty(applicatorName))
                    {
                        applicatorName = UserAPI.UserFullName;
                    }
                }
                if (string.IsNullOrEmpty(summary))
                {
                    summary = TaskAPI.Summary.Split('|')[0];
                }
                l_summary.Text = summary;
                //l_ApplicationNo.Text = applicationNo + "[" + TaskAPI.Incident + "]";
                var list = AssessmentService.FindByPI(TaskAPI.Process, TaskAPI.Incident);
                if (list == null) list = new List<t_Assessment>();
                try
                {
                    l_ApplicationNo.Text = list.FirstOrDefault().BusinessKey + "[" + TaskAPI.Incident + "]";
                }
                catch
                {
                    l_ApplicationNo.Text = "[" + TaskAPI.Incident + "]";
                }
                l_Date.Text = date;
                l_ApplicatorName.Text = applicatorName;

                if (TaskAPI.Process == "New Store Application")
                {
                    lb_eapprove_tip.Visible = true;
                    //lb_eapprove_tip.Text = isEapprove ? "（SMD发起）" : "（非SMD发起）";
                    lb_eapprove_tip.Text = isEapprove ? "（E-approve发起）" : "（非E-approve发起）";
                }
            }
        }
    }
}